<?php

/**
 * @package docBookCompiler
 * @author Vladimir Krivosheev
 * @version $Id: Example_Parser_DocBookCompiler.php 10 2006-10-22 08:22:25Z develar $
 */
class Example_Parser_docBookCompiler
{
	protected $Parser;
	protected $currentExampleId;

	/**
	 * @todo это конфиг и ему здесь не место
	 */
	protected $standartExampleLanguages = array('tpl',  'php');

	public function __construct(Parser_docBookCompiler $Parser)
	{
		$this->Parser = $Parser;
		$examples = $this->Doc->getElementsByTagName('example');

		for ($i = 0; $i < $examples->length; $i++)
		{
			$Example = $examples->item($i);
			$this->currentExampleId = $this->Parser->getIdAttribute($Example);
			if ($this->currentExampleId === '')
			{
				$this->Parser->logicError('You should specify id for example');
			}
			else
			{
				// если example содержит только заголовок, значит мы автоматически генерируем тело примера
				if ($Example->childNodes->length === 3 AND $Example->childNodes->item(0)->nodeType == XML_TEXT_NODE AND $Example->childNodes->item(2)->nodeType == XML_TEXT_NODE AND $Example->childNodes->item(1)->nodeName == 'title')
				{
					$this->Parser->setStandart($Example);
				}
				else
				{
					$this->Parser->setCustom($Example);
				}
				// мы должны установить для example полный уникальный id
				$this->Parser->setIdAttribute($Example, 'example.' . $this->currentExampleId);
			}
		}
		$example_dir = $this->Parser->dir . DIRECTORY_SEPARATOR . self::DIR_EXAMPLES . DIRECTORY_SEPARATOR . $this->currentExampleId;
	}

	protected function setStandart(DOMNode $Example)
	{
		$programlistings = $this->Doc->getElementsByTagName('programlisting');
		for ($i = 0; $i < $programlistings->length; $i++)
		{
			$ProgramListing = $programlistings->item($i);
			$programlisting_id = $this->getIdAttribute($ProgramListing);
			$programlisting_language = $ProgramListing->getAttribute('language');
			if ($programlisting_language === '')
			{
				$this->Parser->logicError('You should specify attribute "language" for "programlisting" in example  ' . $this->currentExampleId);
			}
			if ($programlisting_id === '')
			{
				$program_file_name = $programlisting_language;
			}
			else
			{
				$program_file_name = $programlisting_id . '.' . $programlisting_language;
			}
			$ProgramListing->appendChild($this->Parser->Doc->createCDATASection(file_get_contents($example_dir . DIRECTORY_SEPARATOR . $program_file_name)));
		}
	}

	protected function setCustom(DOMNode $Example)
	{
		if (file_exists($example_dir))
		{
			$example_files = array_intersect($this->Parser->standartExampleLanguages, scandir($example_dir));
			if (empty($example_files))
			{
				$this->Parser->logicError('There are no files for example ' . $this->currentExampleId);
			}
			else
			{
				foreach ($example_files as $program_language)
				{
					$ProgramListing = $this->Parser->Doc->createElement('programlisting');
					$ProgramListing->setAttribute('language', $program_language);
					$ProgramListing->appendChild($this->Parser->Doc->createCDATASection(file_get_contents($example_dir . DIRECTORY_SEPARATOR . $program_language)));

					$Example->appendChild($ProgramListing);
				}

				if (file_exists($example_dir . DIRECTORY_SEPARATOR . 'output'))
				{
					$Screen = $this->Parser->Doc->createElement('screen');
					$Screen->appendChild($this->Parser->Doc->createCDATASection(file_get_contents($example_dir . DIRECTORY_SEPARATOR . 'output')));

					$Example->appendChild($Screen);
				}
				else
				{
					$this->Parser->logicError('There is no file of output for example ' . $this->currentExampleId);
				}
			}
		}
		else
		{
			$this->Parser->logicError('There is no catalog for example ' . $this->currentExampleId);
		}
	}
}

?>